package com.uzai.console.service.orderpull.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.UzaiRespVo;
import com.uzai.console.dto.feign.tborder.TbOrderDownloadDto;
import com.uzai.console.dto.feign.tborder.TbOrderRefundDownloadDto;
import com.uzai.console.dto.orderpull.OrderPullIdDto;
import com.uzai.console.dto.orderpull.OrderPullIdListDto;
import com.uzai.console.dto.orderpull.OrderPullQuery;
import com.uzai.console.entity.OrderPull;
import com.uzai.console.enums.OrderPullPlatformEnum;
import com.uzai.console.enums.OrderPullTbRefundTypeEnum;
import com.uzai.console.enums.OrderPullTbTypeEnum;
import com.uzai.console.mapper.OrderPullMapper;
import com.uzai.console.service.feign.TbOrderFeignService;
import com.uzai.console.service.orderpull.OrderPullService;
import com.uzai.console.vo.orderpull.OrderPullVo;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单拉取
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class OrderPullServiceImpl implements OrderPullService {

    Logger logger = LoggerFactory.getLogger(OrderPullServiceImpl.class);

    @Autowired
    private OrderPullMapper orderPullMapper;
    @Autowired
    private TbOrderFeignService tbOrderFeignService;

    /**
     * 查询订单拉取列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param orderPullQuery
     * @return null
     */
    public Page<OrderPullVo> findOrderPullList(OrderPullQuery orderPullQuery){

        //新建返回到页面的LIST对象
        List<OrderPullVo> orderPullInfoVoList = new ArrayList<>();

        //执行分页查询对象
        Page<OrderPull> page = new Page<>();
        orderPullQuery.convert(page);
        List<OrderPull> orderPullList = orderPullMapper.findByList(page, orderPullQuery);
        if(orderPullList != null){
            for (OrderPull orderPull : orderPullList){
                OrderPullVo orderPullVo = new OrderPullVo();
                BeanUtils.copyProperties(orderPull, orderPullVo);

                //查询订单下载平台类型名称
                if(StringUtils.isNotBlank(orderPull.getPlatform())){
                    OrderPullPlatformEnum orderPullPlatformEnum = OrderPullPlatformEnum.getByKey(orderPull.getPlatform());
                    if(orderPullPlatformEnum != null){
                        orderPullVo.setPlatformName(orderPullPlatformEnum.getDesc());
                    }
                }

                //订单类型
                if(orderPull.getType() != null) {
                    if(orderPull.getPlatform().equals(OrderPullPlatformEnum.TBWQ.getKey())){
                        OrderPullTbRefundTypeEnum orderPullTbRefundTypeEnum = OrderPullTbRefundTypeEnum.getByType(orderPull.getType());
                        if(orderPullTbRefundTypeEnum != null){
                            orderPullVo.setTypeName(orderPullTbRefundTypeEnum.getDesc());
                        }
                    }else if(orderPull.getPlatform().equals(OrderPullPlatformEnum.TB.getKey())){
                        OrderPullTbTypeEnum orderPullTbTypeEnum = OrderPullTbTypeEnum.getByType(orderPull.getType());
                        if(orderPullTbTypeEnum != null){
                            orderPullVo.setTypeName(orderPullTbTypeEnum.getDesc());
                        }
                    }
                }

                orderPullInfoVoList.add(orderPullVo);
            }
        }
        //返回到页面分页对象
        Page<OrderPullVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(orderPullInfoVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 查询订单拉取详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param orderPullIdDto
     * @return null
     */
    public OrderPullVo findOrderPull(OrderPullIdDto orderPullIdDto){
        //返回到页面的VO对象
        OrderPullVo orderPullVo = new OrderPullVo();

        if(orderPullIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        OrderPull orderPull = orderPullMapper.selectById(orderPullIdDto.getId(), orderPullIdDto.getMerId());

        if(orderPull == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该记录不存在");
        }

        //将查询到的数据复制到页面VO中
        BeanUtils.copyProperties(orderPull, orderPullVo);

        //查询订单下载平台类型名称
        if(StringUtils.isNotBlank(orderPull.getPlatform())){
            OrderPullPlatformEnum orderPullPlatformEnum = OrderPullPlatformEnum.getByKey(orderPull.getPlatform());
            if(orderPullPlatformEnum != null){
                orderPullVo.setPlatformName(orderPullPlatformEnum.getDesc());
            }
        }

        //订单类型
        if(orderPull.getType() != null) {
            if(orderPull.getPlatform().equals(OrderPullPlatformEnum.TBWQ.getKey())){
                OrderPullTbRefundTypeEnum orderPullTbRefundTypeEnum = OrderPullTbRefundTypeEnum.getByType(orderPull.getType());
                if(orderPullTbRefundTypeEnum != null){
                    orderPullVo.setTypeName(orderPullTbRefundTypeEnum.getDesc());
                }
            }else if(orderPull.getPlatform().equals(OrderPullPlatformEnum.TB.getKey())){
                OrderPullTbTypeEnum orderPullTbTypeEnum = OrderPullTbTypeEnum.getByType(orderPull.getType());
                if(orderPullTbTypeEnum != null){
                    orderPullVo.setTypeName(orderPullTbTypeEnum.getDesc());
                }
            }
        }

        return  orderPullVo;
    }

    /**
     * 删除订单拉取
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param orderPullIdListDto
     * @return null
     */
    @Transactional
    public void batchDeleteOrderPull(OrderPullIdListDto orderPullIdListDto){

        if(orderPullIdListDto.getIdList() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id列表");
        }
        orderPullMapper.batchDelete(orderPullIdListDto.getIdList());
    }

    /**
     * 订单拉取
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param orderPull
     * @return null
     */
    public void autoOrderPull(OrderPull orderPull){

        if(orderPull == null){
            return;
        }

        try {
            //淘宝维权订单下载
            if(OrderPullPlatformEnum.TBWQ.getKey().equals(orderPull.getPlatform())){
                TbOrderRefundDownloadDto tbOrderRefundDownloadDto = JSONObject.parseObject(orderPull.getParaData(), TbOrderRefundDownloadDto.class);
                if(tbOrderRefundDownloadDto != null){
                    //请求订单拉取接口
                    UzaiRespVo<Boolean> uzaiRespVo_channal = tbOrderFeignService.tbOrderRefundDownload(tbOrderRefundDownloadDto);
                    logger.info("--xxljob自动通过API拉取淘宝渠道维权订单-para={},result={}", JSONObject.toJSONString(tbOrderRefundDownloadDto),JSONObject.toJSONString(uzaiRespVo_channal));
                }
            }else if(OrderPullPlatformEnum.TB.getKey().equals(orderPull.getPlatform())){
                TbOrderDownloadDto tbOrderDownloadDto = JSONObject.parseObject(orderPull.getParaData(), TbOrderDownloadDto.class);
                if(tbOrderDownloadDto != null){
                    //请求订单拉取接口
                    UzaiRespVo<Boolean> uzaiRespVo = tbOrderFeignService.tbOrderDownload(tbOrderDownloadDto);
                    logger.info("---xxljob自动通过API拉取淘宝订单-para={},result={}", JSONObject.toJSONString(tbOrderDownloadDto),JSONObject.toJSONString(uzaiRespVo));
                }
            }
        }catch (Exception e){

        }

        //自动删除该任务
        orderPullMapper.deleteById(orderPull.getId(), orderPull.getMerId());

    }

}
